<?php
/***************************************************************
* Subs-Files.php                                               *
*--------------------------------------------------------------*
* SMF Mod Creator                                              *
* Version 0.1 Alpha                                            *
* Written By: Labradoodle-360                                  *
*--------------------------------------------------------------*
* Copyright 2011 Matthew Kerle - All Rights Reserved           *
*--------------------------------------------------------------*
* Redistribution and use in source and binary forms, with or   *
* without modification, are permitted provided that the        *
* conditions specified in the attached                         *
* smfmodcreator_license.txt file are met.                      *
***************************************************************/
if (!defined('SMF'))
	die ('Hacking Attempt...');

/* This file logs the creation of a file */
function log_file_creation($modID = '', $file_name = '', $file_type = '', $id_dir = '', $author_id = '', $timestamp = '')
{

	// Globalize everything we'll need...
	global $boardurl, $boarddir, $modifications, $smcFunc;

	// Empty requirements? SCREW YOU!!!
	if (empty($modID) || empty($file_name) || empty($file_type) || empty($author_id) || empty($timestamp))
		return;

	// !! Should use in_array or isset later on to validate the directory id.
	$id_dir = !empty($id_dir) ? $id_dir : '0';

	// Actually log it...
	$smcFunc['db_insert']('insert',
		'{db_prefix}modification_files',
		array(
			'file_name' => 'string',
			'id_modification' => 'int',
			'file_type' => 'string',
			'id_directory' => 'int',
			'author_id' => 'int',
			'submitted' => 'int',
		),
		array(
			$file_name,
			$modID,
			$file_type,
			$id_dir,
			$author_id,
			$timestamp,
		),
		array('id_file')
	);			
			
}

/* This function returns an array of the associated mod ids files */
function mod_files($modID = '')
{

	// Globalize everything we'll need...
	global $modifications, $smcFunc, $txt, $mod_files, $memberContext, $scripturl;

	// Is this legit?
	if (empty($modID) || !empty($modID) && !in_array($modID, array_keys($modifications)))
	{
		$mod_files = array();
		return $mod_files;
	}

	// Our Query
	$query = $smcFunc['db_query']('', '
		SELECT f.id_file, f.file_name, f.id_modification,
			f.id_version, f.file_type, f.id_directory, f.author_id,
			f.submitted, f.modified, f.modified_id
		FROM {db_prefix}modification_files AS f
		WHERE id_modification = {int:modID}',
		array(
			'modID' => (int) $modID,
		)
	);

	// Our Results
	while ($data = $smcFunc['db_fetch_assoc']($query))
	{
		// Author Name & ID
		if (!isset($memberContext[$data['author_id']]))
		{
			loadMemberData(array($data['author_id']), false, 'normal');
			loadMemberContext($data['author_id']);
		}

		// Modified Name, ID, & Time
		$data['modified'] = empty($data['modified']) || $data['submitted'] == $data['modified'] ? $txt['smfmc_never'] : timeformat($data['modified']);
		if (!empty($data['modified_id']) && $data['modified_id'] != $data['author_id'] && $data['submitted'] != $data['modified'] && !isset($memberContext[$data['modified_id']]))
		{
			loadMemberData(array($data['modified_id']), false, 'normal');
			loadMemberContext($data['modified_id']);
			$modified_colour = !empty($memberContext[$data['modified_id']]['group_color']) ? ' style="color: ' . $memberContext[$data['modified_id']]['group_color'] . ';"' : '';
			$data['modified'] .= ' <strong>' . $txt['by'] . '</strong> <a href="' . $scripturl . '?action=profile;u=' . $data['modified_id'] . '" target="_self" title="' . $memberContext[$data['modified_id']]['name'] . '"' . $modified_colour . '>' . $memberContext[$data['modified_id']]['name'] . '</a>';
		}

		// Some logic - I love logic!
		$logic['group_color'] = !empty($memberContext[$data['author_id']]['group_color']) ? ' style="color: ' . $memberContext[$data['author_id']]['group_color'] . ';"' : '';

		// Create our array...
		$mod_files[$data['id_file']] = array(
			'name' => $data['file_name'],
			'id_modification' => $data['id_modification'],
			'version' => $data['id_version'],
			'type' => $data['file_type'],
			'id_directory' => $data['id_directory'],
			'author' => '<a href="' . $scripturl . '?action=profile;u=' . $data['author_id'] . '" target="_self" title="' . $memberContext[$data['author_id']]['name'] . '"' . $logic['group_color'] . '>' . $memberContext[$data['author_id']]['name'] . '</a>',
			'submitted' => timeformat($data['submitted']),
			'modified' => $data['modified'],
		);
	}
	$smcFunc['db_free_result']($query);
	return $mod_files;

}

/* This function removes a file */
function removeFile($modID = '', $fileID = '')
{

	// Globalize everything we'll need...
	global $modifications, $smcFunc, $mod_files, $boarddir;

	// Is this legit?
	if (empty($modID) || empty($fileID))
		return;
	elseif (!empty($modID) && !in_array($modID, array_keys($modifications)) || !empty($modID) && !in_array($fileID, array_keys($mod_files)))
		fatal_error('Sorry! The modification could not be deleted. Please try again.', false);

	// First, we delete the actual file.
	unlink($boarddir . '/smfmodcreator/modifications/' . $modifications[$modID] . '/' . $mod_files[$fileID]['name'] . '.' . $mod_files[$fileID]['type']);

	// Finally, we delete the log in the database.
	$smcFunc['db_query']('', '
		DELETE FROM {db_prefix}modification_files
		WHERE id_file = {int:fileID}',
		array(
			'fileID' => (int) $fileID,
		)
	);

}

/* This function retrieves all clean SMF files */
function retrieveSMFFiles()
{

	// Globalize everything we'll need...
	global $smf_files, $boarddir;

	// Our Array
	$smf_files = array(
		'directories' => array(
			'attachments' => array(
				'directories' => array(
				),
				'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/attachments'),
			),
			'avatars' => array(
				'directories' => array(
					'Actors' => array(
						'directories' => array(
						),
						'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/avatars/Actors'),
					),
					'Musicians' => array(
						'directories' => array(
						),
						'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/avatars/Musicians'),
					),
				),
				'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/avatars'),
			),
			'cache' => array(
				'directories' => array(
				),
				'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/cache'),
			),
			'Packages' => array(
				'directories' => array(
					'backups' => array(
						'directories' => array(
						),
						'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Packages/backups'),
					),
				),
				'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Packages'),
			),
			'Smileys' => array(
				'directories' => array(
					'aaron' => array(
						'directories' => array(
						),
						'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Smileys/aaron'),
					),
					'akyhne' => array(
						'directories' => array(
						),
						'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Smileys/akyhne'),
					),
					'default' => array(
						'directories' => array(
						),
						'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Smileys/default'),
					),
				),
				'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Smileys'),
			),
			'Sources' => array(
				'directories' => array(
				),
				'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Sources'),
			),
			'Themes' => array(
				'directories' => array(
					'core' => array(
						'directories' => array(
							'css' => array(
								'directories' => array(
								),
								'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/core/css'),
							),
							'images' => array(
								'directories' => array(
									'admin' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/core/images/admin'),
									),
									'bbc' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/core/images/bbc'),
									),
									'buttons' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/core/images/buttons'),
									),
									'english' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/core/images/english'),
									),
									'icons' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/core/images/icons'),
									),
									'post' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/core/images/post'),
									),
									'topic' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/core/images/topic'),
									),
								),
								'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/core/images'),
							),
							'languages' => array(
								'directories' => array(
								),
								'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/core/languages'),
							),
							'scripts' => array(
								'directories' => array(
								),
								'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/core/scripts'),
							),
						),
						'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/core'),
					),
					'default' => array(
						'directories' => array(
							'css' => array(
								'directories' => array(
								),
								'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default/css'),
							),
							'fonts' => array(
								'directories' => array(
									'Candice' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default/fonts/Candice'),
									),
									'Hootie' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default/fonts/Hootie'),
									),
									'President' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default/fonts/President'),
									),
									'sound' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default/fonts/sound'),
									),								
								),
								'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default/fonts'),
							),
							'images' => array(
								'directories' => array(
									'admin' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default/images/admin'),
									),
									'bbc' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default/images/bbc'),
									),
									'buttons' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default/images/buttons'),
									),
									'english' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default/images/english'),
									),
									'icons' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default/images/icons'),
									),
									'post' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default/images/post'),
									),
									'topic' => array(
										'directories' => array(
										),
										'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default/images/topic'),
									),
								),
								'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default/images'),
							),
							'languages' => array(
								'directories' => array(
								),
								'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default/languages'),
							),
							'scripts' => array(
								'directories' => array(
								),
								'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default/scripts'),
							),
						),
						'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes/default'),
					),
				),
				'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files/Themes'),
			),
		),
		'files' => scandir($boarddir . '/smfmodcreator/resources/smf_files'),
	);

	// An annoying, but crucial fix.
	foreach ($smf_files['directories'] as $id_dir => $directory)
	{
		if (!empty($smf_files['directories'][$id_dir]['files']))
		{
			unset(
				$smf_files['directories'][$id_dir]['files'][0],
				$smf_files['directories'][$id_dir]['files'][1],
				// !! 2 should be removed for any official released, as it's a .svn directory.
				$smf_files['directories'][$id_dir]['files'][2]
			);
		}
		if (!empty($smf_files['directories'][$id_dir]['directories']))
		{
			foreach ($smf_files['directories'][$id_dir]['directories'] as $id_dir2 => $directory2)
			{
				if (!empty($smf_files['directories'][$id_dir]['directories'][$id_dir2]['directories']))
				{
					foreach ($smf_files['directories'][$id_dir]['directories'][$id_dir2]['directories'] as $id_dir3 => $directory3)
					{
						if (!empty($smf_files['directories'][$id_dir]['directories'][$id_dir2]['directories'][$id_dir3]['directories']))
						{
							foreach ($smf_files['directories'][$id_dir]['directories'][$id_dir2]['directories'][$id_dir3]['directories'] as $id_dir4 => $directory4)
							{
								if (!empty($smf_files['directories'][$id_dir]['directories'][$id_dir2]['directories'][$id_dir3]['directories'][$id_dir4]['files']))
								{
									unset(
										$smf_files['directories'][$id_dir]['directories'][$id_dir2]['directories'][$id_dir3]['directories'][$id_dir4]['files'][0],
										$smf_files['directories'][$id_dir]['directories'][$id_dir2]['directories'][$id_dir3]['directories'][$id_dir4]['files'][1],
										// !! 2 should be removed for any official released, as it's a .svn directory.
										$smf_files['directories'][$id_dir]['directories'][$id_dir2]['directories'][$id_dir3]['directories'][$id_dir4]['files'][2]
									);
								}
							}
						}
						if (!empty($smf_files['directories'][$id_dir]['directories'][$id_dir2]['directories'][$id_dir3]['files']))
						{
							unset(
								$smf_files['directories'][$id_dir]['directories'][$id_dir2]['directories'][$id_dir3]['files'][0],
								$smf_files['directories'][$id_dir]['directories'][$id_dir2]['directories'][$id_dir3]['files'][1],
								// !! 2 should be removed for any official released, as it's a .svn directory.
								$smf_files['directories'][$id_dir]['directories'][$id_dir2]['directories'][$id_dir3]['files'][2]
							);
						}
					}
				}
				if (!empty($smf_files['directories'][$id_dir]['directories'][$id_dir2]['files']))
				{
					unset(
						$smf_files['directories'][$id_dir]['directories'][$id_dir2]['files'][0],
						$smf_files['directories'][$id_dir]['directories'][$id_dir2]['files'][1],
						// !! 2 should be removed for any official released, as it's a .svn directory.
						$smf_files['directories'][$id_dir]['directories'][$id_dir2]['files'][2]
					);
				}
			}
		}
	}
	if (!empty($smf_files['files']))
	{
		unset(
			$smf_files['files'][0],
			$smf_files['files'][1],
			// !! 2 should be removed for any official released, as it's a .svn directory.
			$smf_files['files'][2]
		);
	}

}

?>